/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.ipreview;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import org.idea.utils.sfo.SFODocManager;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//idea.ipreview//IPreview//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "IPreviewTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(position = 4, mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "idea.ipreview.IPreviewTopComponent")
@ActionReferences(
        {
    @ActionReference(path = "Menu/Window", position = 933),
    @ActionReference(path = "Menu/File/Open", position = 1800)
})
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_IPreviewAction",
        preferredID = "IPreviewTopComponent")
@Messages(
        {
    "CTL_IPreviewAction=Preview",
    "CTL_IPreviewTopComponent=Preview Window",
    "HINT_IPreviewTopComponent=This is a Preview window"
})
public final class IPreviewTopComponent extends TopComponent {

    public IPreviewTopComponent() {
        initComponents();
        setName(Bundle.CTL_IPreviewTopComponent());
        setToolTipText(Bundle.HINT_IPreviewTopComponent());
        setLayout(new BorderLayout());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        imageLabel = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(imageLabel, org.openide.util.NbBundle.getMessage(IPreviewTopComponent.class, "IPreviewTopComponent.imageLabel.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(101, 101, 101)
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(131, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(120, 120, 120)
                .addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(133, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel imageLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    /**
     *
     * @param sdm the SFO Doc Manager
     */
    public void displayPreview(SFODocManager sdm) {
        removeAll(); // clear previous previews
        ImageIcon image = sdm.getJpegImage();
        imageLabel = new JLabel("", image, JLabel.CENTER);

        //jPanel1.setLayout(new BorderLayout());

        jScrollPane1.getViewport().add(imageLabel);
        add(jScrollPane1, BorderLayout.CENTER);

    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}
